import {
  UserModel
} from '../../../models/user.js';

const userModel = new UserModel();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {
      id:0,
      province_id: 0,
      city_id: 0,
      district_id: 0,
      address: '',
      full_region: '',
      name: '',
      mobile: '',
      is_default: 0
    },
    addressId: 0,
    Region: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        addressId: options.id
      });
      this.getAddressDetail();
    }
  },
 
  getAddressDetail: function() {
    userModel.getAddressDetail(this.data.addressId).then(res => {
      if (res.errno === 0) {
        // console.log(res.data)
        this.setData({
          address: res.data,
          region: res.data.full_region
        });
        console.log(res.data)
      }
    })
  },

  /* 修改地区 */
  bindRegionChange: function(event) {
    console.log(event)
    this.setData({
      full_region: event.detail.value.join(''),
      region: event.detail.value.join('')
    })
  },
  
  bindIsDefault() {
    let address = this.data.address;
    address.is_default = !address.is_default;
    this.setData({
      address: address
    });
  },

  cancelAddress: function() {
    wx.navigateTo({
      url: '/pages/shopping/checkout/index',
    })
  },

  saveAddress: function(event) {
    console.log(event);
    let p_values = event.detail.value

    // 表单验证
    if (!this.validation(p_values)) {
      wx.showModal({
        title: '温馨提示',
        content: this.data.error,
        showCancel: false
      })
      return false;
    }

    let p_id = this.data.address.id;
    let p_name = this.data.address.name;
    let p_mobile = this.data.address.mobile;
    let p_province_id = this.data.address.province_id;
    let p_city_id = this.data.address.city_id;
    let p_district_id = this.data.address.district_id;
    let p_address = this.data.address.address;
    let p_is_default = this.data.address.is_default;

    userModel.getAddressSave(p_id, p_name, p_mobile, p_province_id, p_city_id, p_district_id, p_address, p_is_default).then(res => {
      if (res.errno === 0) {
        wx.showToast({
          title: '修改成功',
        })
        setTimeout(() => {
          wx.navigateBack({
            // url: '/pages/shopping/address/index'
            delta: 1
          })
        }, 1000);
      }
    })
  },

  /**
   * 表单验证
   */
  validation: function(values) {
    if (values.name === '') {
      this.data.error = '收件人不能为空';
      return false;
    }
    if (values.mobile === '') {
      this.data.error = '手机号不能为空';
      return false;
    }
    if (values.mobile.length !== 11) {
      this.data.error = '手机号长度有误';
      return false;
    }
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!reg.test(values.mobile)) {
      this.data.error = '手机号不符合要求';
      return false;
    }
    if (this.data.full_region === '') {
      this.data.error = '省市区不能空';
      return false;
    }
    if (values.address === '') {
      this.data.error = '详细地址不能为空';
      return false;
    }
    return true;
  },
})